package com.example.xyr.myapplication.utils

import android.content.Context
import android.text.SpannableStringBuilder
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.sdsmdg.tastytoast.TastyToast

/**
 * Created by ylcf on 2018/8/23.
 */
class TextUtil {

    private constructor()

    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            TextUtil()
        }
    }

    fun getColorText(str: String, color: Int, start: Int, end: Int, listener: ClickableSpanLis): SpannableStringBuilder {
        val spanPink = ForegroundColorSpan(color)
        val builder = SpannableStringBuilder(str)
        //设置部分字体点击事件
        val clickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                listener.onClick(widget)
            }
        }
        builder.setSpan(spanPink, start, end, SpannableStringBuilder.SPAN_INCLUSIVE_EXCLUSIVE)
        builder.setSpan(clickableSpan, start, end, SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE)
        return builder
    }


    fun setTextColor(text: String, color: Int, num: Int): SpannableStringBuilder {
        val builder = SpannableStringBuilder(text)
        val span = ForegroundColorSpan(color)
        builder.setSpan(span, 0, num, SpannableStringBuilder.SPAN_INCLUSIVE_EXCLUSIVE)
        return builder
    }

    fun judgePass(context: Context, old: String, new: String, confirm: String): Boolean {
        if (StringUtil.isEmpty(old) || StringUtil.isEmpty(new) || StringUtil.isEmpty(confirm)) {
            TastyToast.makeText(context, "请输全密码", TastyToast.LENGTH_LONG, TastyToast.WARNING)
            return true
        } else if (old == new) {
            TastyToast.makeText(context, "旧密码和新密码相同", TastyToast.LENGTH_LONG, TastyToast.WARNING)
            return true
        } else if (new != confirm) {
            TastyToast.makeText(context, "确认密码与新密码不同", TastyToast.LENGTH_LONG, TastyToast.WARNING)
            return true
        } else {
            return false
        }
    }

    interface ClickableSpanLis {
        fun onClick(widget: View)
    }
}